package com.app.xinfadi.ui.purchase_plan.produingAreaPrice;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseFragment;
import com.app.xinfadi.dao_bean.bean.PurchasePlanBean;
import com.app.xinfadi.ui.purchase_plan.view.ProducingAreaPriceListAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * function: 产地采购报价 子栏目内容
 * Created by Wutengfei on 2016/1/25.
 */
public class ProducingAreaPriceListFragment extends BaseFragment {
    private View view;
    private PullToRefreshListView pullToRefreshListView;
    private ProducingAreaPriceListAdapter producingAreaPriceListAdapter;

    public static ProducingAreaPriceListFragment getInstance(int index) {
        ProducingAreaPriceListFragment instance = new ProducingAreaPriceListFragment();
        Bundle args = new Bundle();
//      args.putString("status", status);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.pulltorefresh_listview_fragment, null);
        initView();
        initData();
        return view;
    }

    private void initData() {

    }

    private void initView() {
        initListView();
    }

    private void initListView() {
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pullToRefreshListView);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(context, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                        | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                if (PullToRefreshBase.Mode.PULL_FROM_START == pullToRefreshListView.getCurrentMode()) {
//                    Log.e("pull_down", "up");
//                    pageNo = 1;
//                    taskAdapter.getData().clear();
//                    initListViewData();
                    new GetDataTask().execute();
                } else if (PullToRefreshBase.Mode.PULL_FROM_END == pullToRefreshListView.getCurrentMode()) {
//                    Log.e("pull_down", "down");
                }
//                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            }
        });
        producingAreaPriceListAdapter = new ProducingAreaPriceListAdapter(context);
        pullToRefreshListView.setAdapter(producingAreaPriceListAdapter);

        initListViewData();
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, ProducingAreaPriceDetailActrvity.class);
                startActivity(intent);
            }
        });
    }

    private void initListViewData() {
        String myPatientsRingMessage[] = {"黄瓜", "西红柿", "青椒", "土豆", "油", "白菜", "胡萝卜", "大豆", "海带", "香菜"};
        List<PurchasePlanBean> purchasePlanBeanList = new ArrayList<PurchasePlanBean>();
        // 已经选的 49
        for (int i = 0; i < myPatientsRingMessage.length * 1; i++) {
            PurchasePlanBean purchasePlanBean = new PurchasePlanBean();
            purchasePlanBean.purchaseName = myPatientsRingMessage[i % 10];
            purchasePlanBeanList.add(purchasePlanBean);
        }
        refreshAdapter(purchasePlanBeanList);
    }

    private void refreshAdapter(List<PurchasePlanBean> purchasePlanBeanList) {
        producingAreaPriceListAdapter.getData().addAll(purchasePlanBeanList);
        producingAreaPriceListAdapter.notifyDataSetChanged();
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pullToRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}
